Creating a Telegram bot and sending messages using Github actions |
您所在的位置:网站首页 › github action telegram › Creating a Telegram bot and sending messages using Github actions |
Creating a Telegram bot and sending messages using Github actions![]() Rakshith Jayakumar ·Follow 3 min read·Nov 5, 2023-- Recently i had a requirement to priodically monitor coffee market for price fluctuations, and subsequently giving alerts to my village community of the same. After thinking of various approaches, i zeroed in on creating a Telegram bot that would update users of the price daily This article talks about the various steps involved in creating a bot and proceeds to talk about plugging it with Github actions to solve my problem statement Phase 1Creating a Telegram BotOpen the Telegram app and search for the “BotFather” bot. Start a chat with Botfather and send /newbot, this brings up a command prompt which asks for bot name and couple other instructions. Follow the prompt to complete initialisation of your bot After successfully completing the steps, Botfather would send you a confirmation message which would also contain the API token. That’s it… the Bot is now active. You can search for the bot ( @bot_name ), send messages to it as well as add it to groups. Sending messages via the BotNow that the bot has been successfully, let’s configure the Bot to send messages Add the Bot to the required groupI created a new Group and added the bot to it, the next step was to test the bot has all necessary permissions for the sending messages. 2. Find the group id for which messages has to be sent https://api.telegram.org/botXXX:YYYY/getUpdates ( XXX:YYYY denotes your API token ) The above URL would display a serious of objects. All these indicate the different messages received by the bot. Search for and id key in the object {“chat”:{“id”:-zzzzzzzzzz.. }} zzzzzzzzzz is your chat id (with the negative sign). Group ids are negative, while personal chats are notnote — ensure group_name matches your desired group name 3. Send a test message curl -X POST "https://api.telegram.org/botXXX:YYYY/sendMessage" -d "chat_id=-zzzzzzzzzz&text=A simple test message"The bot should send the message in the group. In case the message failed to deliver, cross check the id as well as the permissions of the bot in group. That’s it.. we’ve now created a bot that can successfully send messages to a group 🎉 🎉 Bonus — sending images To send images, curl -X POST "https://api.telegram.org/botXXX:YYYY/sendPhoto"This takes chat_id and caption as params. In the post call, also include the images under photo argument A full list of capabilities of the API as well as other API functionality can be accessed here — https://core.telegram.org/bots/api#sendphoto Phase 2I cover in detail on the steps taken to solve my problem statement in the subsequent article Github actions — Setup and TweaksThismedium.com But extracting content on a high level, using Python, this is how i send messages/images Sending a text message 👇 def send_telegram_message(text_message): base_url = f'https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}' send_message_url = f'{base_url}/sendMessage'data = { 'chat_id': TELEGRAM_CHAT_ID, 'text': text_message, } response = requests.post(send_message_url, data=data) if response.status_code == 200: print("Message sent successfully.") else: print(f"Failed to send message. Status code: {response.status_code}") Sending images 👇 def send_graph_as_photo(filename, graph_buffer): base_url = f'https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}' send_photo_url = f'{base_url}/sendPhoto'files = {'photo': ('graph.png', graph_buffer)} data = { 'chat_id': TELEGRAM_CHAT_ID, 'caption': f'Last 5 days trend {filename}', } response = requests.post(send_photo_url, data=data, files=files) if response.status_code == 200: print(f"Graph sent successfully for {filename}.") else: print( f"Failed to send graph for {filename}. Status code: {response.status_code}") |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |